/**
 * Created by jinji on 2017/12/6.
 */
//滚动条在Y轴上的滚动距离
function getScrollTop(){
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if(document.body){
        bodyScrollTop = document.body.scrollTop;
    }
    if(document.documentElement){
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
    return scrollTop;
}

//文档的总高度
function getScrollHeight(){
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if(document.body){
        bodyScrollHeight = document.body.scrollHeight;
    }
    if(document.documentElement){
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;
}
//浏览器视口的高度
function getWindowHeight(){
    var windowHeight = 0;
    if(document.compatMode == "CSS1Compat"){
        windowHeight = document.documentElement.clientHeight;
    }else{
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}

/**
 *
 * @param obj 监听对象
 * @param type 事件类型
 * @param fn 回调函数
 */
function addEvent(obj,type,fn){
    if(obj.attachEvent){
        obj.attachEvent('on'+type,function(){
            fn.call(obj);
        })
    }else{
        obj.addEventListener(type,fn,false);
    }
}

function buildParams(params) {
    if (typeof(params) == 'undefined') {
        return '';
    } else {
        return '?' + params.join('&')
    }
}
//重写log
var mylog = console.log;
var log = function(obj){
    if(app_debug){
        mylog(obj);
    }
};

/**
 *
 * @param message 消息内容
 * @param type success 成功 '' 提示 warning 警告 error 错误
 * @param obj this vue对象
 */
function sendMessage(message,type,obj) {
    if(type != 'error'){
        obj.$message({
            message: message,
            type: type
        });
    }else{
        obj.$message.error(message);
    }
}

export {
    getScrollTop,
    getScrollHeight,
    getWindowHeight,
    addEvent,
    buildParams,
    log,
    sendMessage,
}
